package io.jft.doll.ui.home.bind

import android.support.v4.view.ViewPager
import android.view.View
import android.view.ViewGroup
import com.g.base.extend.inflateDataBinding
import com.g.base.extend.setTimeout
import com.g.base.ui.recyclerView.item.BaseItem
import io.jft.doll.R
import io.jft.doll.adapter.ImageSliderPageAdapter
import io.jft.doll.databinding.ItemHomeImageSlidePageBinding
import io.jft.doll.databinding.ViewImageBinding
import io.jft.doll.model.bindModel.ImageBind

/**
 * Created by G on 2017/8/16 0016.
 */
class HomeImageSliderBind(var imageSrcList: List<String>? = null) : BaseItem<ItemHomeImageSlidePageBinding>() {
    override val layoutId: Int = R.layout.item_home_image_slide_page
    override val recyclerAble: Boolean = false
    private var viewImageModelList = ArrayList<ImageBind>()
    private var pagerStatus = ViewPager.SCROLL_STATE_IDLE
    private var autoScrollTimeout: (() -> Unit)? = null
    private lateinit var viewpager: ViewPager
    var parentStop = false

    override fun onBind(binding: ItemHomeImageSlidePageBinding) {
        val imageSrcList = imageSrcList ?: return
        //原始路径没有  直接返回
        if (imageSrcList.isEmpty()) return
        //viewPage容器
        val root = binding.root as ViewGroup
        //清除之前的数据
        viewImageModelList.clear()
        //创建ImageModel List
        for (index in imageSrcList.indices) {
            val viewImageBinding = root.inflateDataBinding<ViewImageBinding>(R.layout.view_image)
            val element = ImageBind(imageSrcList[index], viewImageBinding)
            //点击监听 传递给ImageSliderHolder
            element.setOnClickListener {
                //返回 View 和 他是第几个 与ImageScrList对应
                onClickListener?.invoke(it, index)
            }
            //添加到集合
            viewImageModelList.add(element)
        }
        //创建viewPagerAdapter
        val imageSliderPageAdapter = ImageSliderPageAdapter()
        imageSliderPageAdapter.setImageViewList(viewImageModelList)
        //添加到viewPage
        viewpager = binding.viewpager
        viewpager.adapter = imageSliderPageAdapter
        //smartTab绑定viewPage
        binding.smartTabLayout.setViewPager(viewpager)

        viewpager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
            override fun onPageScrollStateChanged(state: Int) {
                pagerStatus = state
            }
        })

        viewpager.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
            override fun onViewDetachedFromWindow(v: View?) {
                finishAutoScroll()
            }

            override fun onViewAttachedToWindow(v: View?) {
                startAutoScroll()
            }
        })
    }

    fun startAutoScroll() {
        if (imageSrcList?.size ?: 0 <= 1) return
        autoScrollTimeout = setTimeout(3000L) {
            if (pagerStatus == ViewPager.SCROLL_STATE_IDLE && !parentStop) {
                var currentItem = viewpager.currentItem + 1
                if (currentItem >= imageSrcList?.size ?: 0)
                    currentItem = 0
                viewpager.currentItem = currentItem
            }
            startAutoScroll()
        }
    }

    fun finishAutoScroll() {
        autoScrollTimeout?.invoke()
    }
}